package com.cy.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author : 残影
 * @Date: 2020/7/22 15:31
 */
public class Consumer_Routing2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2. 设置参数
        //ip  默认值 localhost
        factory.setHost("192.168.1.91");
        //端口  默认值 5672
        factory.setPort(5672);
        //虚拟机 默认值/
        factory.setVirtualHost("/");
        //用户名 默认 guest
        factory.setUsername("guest");
        //密码 默认值 guest
        factory.setPassword("guest");
        //3. 创建连接 Connection
        Connection connection = factory.newConnection();
        //4. 创建Channel
        Channel channel = connection.createChannel();
        String queue1Name = "test_direct_queue1";
        // 接收消息
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body："+new String(body));
                System.out.println("将日志信息存储到数据库.....");
            }
        };
        channel.basicConsume(queue1Name,true,consumer);
        //关闭资源？不要

    }
}
